﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ASPItalia.ModelVirtualCasting.Web.Controllers;
using ASPItalia.ModelVirtualCasting.EntityFramework;
using ASPItalia.ModelVirtualCasting.ObjectModel;
using ASPItalia.ModelVirtualCasting.Web.Tests.Utils;
using ASPItalia.ModelVirtualCasting.Web.Models;
using System.Web.Mvc;

namespace ASPItalia.ModelVirtualCasting.Web.Tests.Controllers
{
    [TestClass]
    public class ModelControllerTest
    {
        [TestMethod]
        public void Search_Applies_Filters()
        {
            // Arrange
            Model first, second, third;
            var models = new List<Model>
                {
                    (first = new Model() { FirstName = "Marco", LastName = "De Sanctis" }),
                    (second = new Model() { FirstName = "Marco", LastName = "Leoncini" }),
                    (third = new Model() { FirstName = "Stefano", LastName = "Mostarda" })
                };
            var objectSet = new FakeDbSet<Model>(models);
            var modelRepository = new ModelRepository(objectSet);
            
            var searchModel = new SearchModel() { Firstname = "M" };
            var controller = new ModelController(modelRepository);

            // Act
            var res = controller.Results(searchModel.Serialize(), null /* first page */)
                as ViewResult;

            // Assert
            Assert.IsNotNull(res);
            var searchModelResult = res.ViewData.Model as SearchModelResult;
            Assert.IsNotNull(searchModelResult);
            Assert.IsTrue(searchModelResult.Items.Contains(first), "First should be a result");
            Assert.IsTrue(searchModelResult.Items.Contains(second), "Second should be a result");
            Assert.IsFalse(searchModelResult.Items.Contains(third), "Third shouldn't be a result");
        }
    }
}
